package org.hhh.org.service.impl;

import org.hhh.basic.exception.BusinessException;
import org.hhh.basic.service.impl.BaseServiceImpl;
import org.hhh.org.domain.Employee;
import org.hhh.org.domain.Shop;
import org.hhh.org.mapper.EmployeeMapper;
import org.hhh.org.mapper.ShopMapper;
import org.hhh.org.service.IEmployeeService;
import org.hhh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlemen(Shop shop) {
        //1.判断密码是否一致
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入");
        }
        //2.检验账号是否已经被注册过 校验账号
        Employee emp=employeeMapper.loadByUserName(admin.getUsername());
        if (emp!=null){
            throw new BusinessException("用户名已存在,请更改");
        }
        //注册
        //1.保存employee信息 返回一个自增的id
        //employeeMapper.save(admin);
        employeeService.add(admin);//业务由自己的业务实现
        //2.给shop保存店长信息  admin_id自增一个店铺信息id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        //3.给employee保存店铺的信息shop_id
        admin.setShop_id(shop.getId());
        //修改信息
        employeeMapper.upadate(admin);


    }
}
